<?php
// +----------------------------------------------------------------------
// | 幕小铺
// +----------------------------------------------------------------------
// | 版权所有 2016~2020 幕小铺科技（大连）有限公司
// +----------------------------------------------------------------------
// | 2020/03/14
// +----------------------------------------------------------------------
namespace app\user_center\behavior\user\paySuccessOrderCz_after;

/**
 * 充值 赠送规则
 * @package app\user_center\behavior\user
 * @author shiqiang
 */
class BalanceRule{
    /**
     * 执行行为 run方法是Behavior唯一的接口
     * @access public
     * @return void
     */
    public function run($params){
		$order_code		= $params['orderid'];
		$out_trade_no 	= $params['out_trade_no'];
		
		//查询订单信息
        $map = [];
        $map['order_code'] = $order_code;
		$ErpGoodsOrderObj = model('UcMemberBalance', 'model\user') -> where($map) -> find();
		$price = $ErpGoodsOrderObj -> pay_price;
		//查询规则表
		$map = [];
		$map['status'] = 1;
		$list = model('UcMemberBalanceRules', 'model\user') -> where($map) ->order('level desc') -> select();
		foreach ($list as $key => $value) {
			if($price >= $value['man_price']){
				//触发赠送 并且跳出
				//增加一条流水记录
				$uid = $ErpGoodsOrderObj -> uid;
				$journal_id = $ErpGoodsOrderObj -> id;
				$ine = 2;
				$price = $value['song_price'];
				$log_remark = $value['name'].',赠送：'.$price;
				model('UcMemberBalanceJournalLog', 'model\user')->addData($uid, $journal_id, $ine, $price, $use_rule='', $log_remark);
				break;//只增金额一次
			}
		}
    }
}
